﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
namespace Space_Conquest
{
    class OnScreenDisplay : IRender, IInput
    {

        #region Member Variables

        GameEngine _parent;
        private Player _player;
        
        bool _inGameMenuVisible = false;
        bool _optionSelected = false;
        bool _builderSelected = false;
        bool _factorySelected = false;
        bool _barrackSelected = false;
        bool _centerSelected = false;
        bool _airStationSelected = false;
        string _mouseOverText = "";
        List<String> musicfiles = new List<string>(Directory.GetFiles(Globals.RESOURCE_DIRECTORY + "Music/InGame/"));
        int j = 0;

        #region InputManagers
            InputManager _inputManager = new InputManager();
            InputManager _inputManagerInGameMenu = new InputManager();
            InputManager _inputManagerOptions = new InputManager();
            InputManager _inputManagerBuilder = new InputManager();
            InputManager _inputManagerFactory = new InputManager();
            InputManager _inputManagerBarrack = new InputManager();
            InputManager _inputManagerCenter = new InputManager();
            InputManager _inputManagerAirStation = new InputManager();
            InputManager _inputManagerPowerPlant = new InputManager();
        #endregion

        InGameButton _inGameMenuButton = new InGameButton(Globals.iScreenWidth, 0, 147, 32, "Images/Menu.bmp");
        Sprite _sprBottomPanel = new Sprite(0, 0, 648, 222, 255, false);
        Texture _texBottomPanel = new Texture("Images/OnScreenDisplayBottomPanel.jpg");

        #region Building Buttons
            InGameButton _townCenter = new InGameButton(480, Globals.iScreenHeight - 125, 110, 30, "Images/BuildButton.bmp");
            InGameButton _factory = new InGameButton(480, Globals.iScreenHeight - 90, 110, 30, "Images/BuildButton.bmp");
            InGameButton _airStation = new InGameButton(480, Globals.iScreenHeight - 55, 110, 30, "Images/BuildButton.bmp");
            InGameButton _barrack = new InGameButton(650, Globals.iScreenHeight - 125, 110, 30, "Images/BuildButton.bmp");
            InGameButton _turret = new InGameButton(650, Globals.iScreenHeight - 90, 110, 30, "Images/BuildButton.bmp");
            InGameButton _powerPlant = new InGameButton(650, Globals.iScreenHeight - 55, 110, 30, "Images/BuildButton.bmp");
        #endregion

        #region Unit Buttons
            InGameButton _laborer = new InGameButton(500, Globals.iScreenHeight - 125, 100, 30, "Images/BuildButton.bmp");
            InGameButton _jeep = new InGameButton(500, Globals.iScreenHeight - 125, 80, 30, "Images/BuildButton.bmp");
            InGameButton _tank = new InGameButton(500, Globals.iScreenHeight - 90, 80, 30, "Images/BuildButton.bmp");
            InGameButton _robot = new InGameButton(500, Globals.iScreenHeight - 90, 80, 30, "Images/BuildButton.bmp");
            InGameButton _soldier = new InGameButton(500, Globals.iScreenHeight - 125, 80, 30, "Images/BuildButton.bmp");
            InGameButton _lightship = new InGameButton(500, Globals.iScreenHeight - 125, 110, 30, "Images/BuildButton.bmp");
            InGameButton _battleship = new InGameButton(500, Globals.iScreenHeight - 90, 110, 30, "Images/BuildButton.bmp");
            InGameButton _generate = new InGameButton(500, Globals.iScreenHeight - 125, 100, 30, "Images/BuildButton.bmp");
        #endregion

        Sprite _sprframeWrapper = new Sprite(0, 0, 600, 112, 255, false);
        Texture _texframeWrapper = new Texture("Images/FrameWrapper.jpg");

        Portrait _portrait;
        Sprite _sprframe = new Sprite(0, 0, 92, 92, 255, false);
        Texture _texframe = new Texture("Images/Frame.jpg");

        #region InGame Menu
            Texture _inGameMenuTexture = new Texture("Images/InGameMenuBackground.bmp");
            Sprite _inGameMenuSprite = new Sprite(((Globals.iScreenWidth /2)- 250), 50, 500, 500, 200, false);
            InGameMenuButtons _continue = new InGameMenuButtons(((Globals.iScreenWidth /2) - 125), 140, 250, 77, "Images/ContinueGame.bmp");
            //InGameMenuButtons _playerbtn = new InGameMenuButtons(((Globals.iScreenWidth / 2) - 125), 160, 250, 77, "Images/Player.bmp");
            InGameMenuButtons _option = new InGameMenuButtons(((Globals.iScreenWidth / 2) - 125), 260, 250, 77, "Images/Option.bmp");
            //InGameMenuButtons _save = new InGameMenuButtons(((Globals.iScreenWidth / 2) - 125), 360, 250, 77, "Images/Save.bmp");
            InGameMenuButtons _exit = new InGameMenuButtons(((Globals.iScreenWidth / 2) - 125), 380, 250, 77, "Images/Exit.bmp");
            InGameMenuButtons _music = new InGameMenuButtons(300, 65, 200, 200, "Images/Music.bmp");
            InGameMenuButtons _musicOn = new InGameMenuButtons(430, 60, 200, 200, "Images/On.bmp");
            InGameMenuButtons _musicOff = new InGameMenuButtons(530, 60, 200, 200, "Images/Off.bmp");
            InGameMenuButtons _sound = new InGameMenuButtons(300, 150, 200, 200, "Images/Sound.bmp");
            InGameMenuButtons _soundOn = new InGameMenuButtons(430, 150, 200, 200, "Images/On.bmp");
            InGameMenuButtons _soundOff = new InGameMenuButtons(530, 150, 200, 200, "Images/Off.bmp");
            InGameMenuButtons _soundVolume = new InGameMenuButtons(300, 230, 200, 200, "Images/Volume.bmp");
            InGameMenuButtons _soundDown = new InGameMenuButtons(530, 290, 200, 200, "Images/Down.bmp");
            InGameMenuButtons _soundUp = new InGameMenuButtons(370, 290, 200, 200, "Images/Up.bmp");
            InGameMenuButtons _back = new InGameMenuButtons(430, 460, 150, 45, "Images/BackButton.bmp");
            InGameMenuButtons _startGame = new InGameMenuButtons(((Globals.iScreenWidth / 2) - 125), 460, 250, 77, "Images/StartButton.bmp");
        #endregion
        
        Sprite _sprTopPanel = new Sprite(0, 0, 738, 32, 255, false);
        Texture _texTopPanel = new Texture("Images/OnScreenDisplayTopPanel.bmp");

        #endregion

        #region Constructor

        public OnScreenDisplay(GameEngine parent)
        {

            _parent = parent;
            _portrait = new Portrait(_parent.LocalPlayer);
            _player = Parent.LocalPlayer;

            _inputManager.Add(_portrait);
            _inputManager.Add(_inGameMenuButton);
            _inGameMenuButton.OnButtonClick += new InGameButtonHandler(_inGameButton_OnButtonClick);

            #region Build Buttons
                _inputManagerBuilder.Add(_townCenter);
                _townCenter.OnButtonClick += new InGameButtonHandler(_townCenter_OnButtonClick);
                _inputManagerBuilder.Add(_factory);
                _factory.OnButtonClick += new InGameButtonHandler(_fatory_OnButtonClick);
                _inputManagerBuilder.Add(_airStation);
                _airStation.OnButtonClick += new InGameButtonHandler(_airStation_OnButtonClick);
                _inputManagerBuilder.Add(_barrack);
                _barrack.OnButtonClick += new InGameButtonHandler(_barrack_OnButtonClick);
                _inputManagerBuilder.Add(_turret);
                _turret.OnButtonClick += new InGameButtonHandler(_turret_OnButtonClick);
                _inputManagerBuilder.Add(_powerPlant);
                _powerPlant.OnButtonClick +=new InGameButtonHandler(_powerPlant_OnButtonClick);
            #endregion

            #region Build Unit Buttons

                _inputManagerCenter.Add(_laborer);
                _laborer.OnButtonClick += new InGameButtonHandler(_laborer_OnButtonClick);
                _inputManagerFactory.Add(_jeep);
                _jeep.OnButtonClick += new InGameButtonHandler(_jeep_OnButtonClick);
                _inputManagerFactory.Add(_tank);
                _tank.OnButtonClick += new InGameButtonHandler(_tank_OnButtonClick);
                _inputManagerBarrack.Add(_robot);
                _robot.OnButtonClick += new InGameButtonHandler(_robot_OnButtonClick);
                _inputManagerBarrack.Add(_soldier);
                _soldier.OnButtonClick += new InGameButtonHandler(_soldier_OnButtonClick);
                _inputManagerAirStation.Add(_lightship);
                _lightship.OnButtonClick += new InGameButtonHandler(_lightship_OnButtonClick);
                _inputManagerAirStation.Add(_battleship);
                _battleship.OnButtonClick += new InGameButtonHandler(_battleship_OnButtonClick);
                _inputManagerPowerPlant.Add(_generate);
                _generate.OnButtonClick += new InGameButtonHandler(_generate_OnButtonClick);
            #endregion

            #region InGame Menu
                _inputManagerInGameMenu.Add(_continue);
                _continue.OnButtonClick += new InGameMenuHandler(_continue_OnButtonClick);
                //_inputManagerInGameMenu.Add(_playerbtn);
                //_playerbtn.OnButtonClick += new InGameMenuHandler(_player_OnButtonClick);
                _inputManagerInGameMenu.Add(_option);
                _option.OnButtonClick += new InGameMenuHandler(_option_OnButtonClick);
                //_inputManagerInGameMenu.Add(_save);
                //_save.OnButtonClick += new InGameMenuHandler(_save_OnButtonClick);
                _inputManagerInGameMenu.Add(_exit);
                _exit.OnButtonClick += new InGameMenuHandler(_exit_OnButtonClick);
            #endregion

            #region Option Menu
            _inputManagerOptions.Add(_musicOn);
                _musicOn.OnButtonClick += new InGameMenuHandler(_musicOn_OnButtonClick);
                _inputManagerOptions.Add(_musicOff);
                _musicOff.OnButtonClick += new InGameMenuHandler(_musicOff_OnButtonClick);
                _inputManagerOptions.Add(_soundOn);
                _soundOn.OnButtonClick += new InGameMenuHandler(_soundOn_OnButtonClick);
                _inputManagerOptions.Add(_soundOff);
                _soundOff.OnButtonClick += new InGameMenuHandler(_soundOff_OnButtonClick);
                _inputManagerOptions.Add(_back);
                _back.OnButtonClick += new InGameMenuHandler(_back_OnButtonClick);
            #endregion

            _startGame.OnButtonClick += new InGameMenuHandler(_startGame_OnButtonClick);
            PlayMusic();
        }

        #endregion

        public void PlayMusic()
        {
            if (j == musicfiles.Count - 1)
            {
                j = 0;
            }
            else
            {
                Audio.PlayMusic("Music/InGame/" + Path.GetFileName(musicfiles[j]));
                j++;
            }
        }
        #region Properties

        public GameEngine Parent
        {
            get
            {
                return _parent;
            }
        }

        #endregion

        #region Event

        void _townCenter_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.T, false);
        }

        public void BuildTownCenter()
        {
            if (Parent.WorkingPlayer.Metal >= 500)
            {
                bool canBuild = true;
                Laborer builder = ((Laborer)Parent.WorkingPlayer.SelectedUnit);
                MapArea newBuildingArea = new MapArea(builder.X + 1, builder.Y, 2, 2);

                foreach (MapPoint point in newBuildingArea.GetListOfAllMapPoints())
                {
                    if (!Parent.Map.TileIsPassable(UnitLayer.Ground, point.X, point.Y))
                    {
                        canBuild = false;
                    }
                }

                if (canBuild)
                {
                    BuildingUnit b = new TownCenter(builder.MapPoint.X + 1, builder.MapPoint.Y, Parent.WorkingPlayer);
                    b.IsBuilt = false;
                    Parent.UnitManager.AddUnit(b);
                    builder.BuildBuilding(b);
                    Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 500;
                }
            }
        }

        void _powerPlant_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.P, false);
        }

        public void BuildPowerPlant()
        {
            if (Parent.WorkingPlayer.Metal >= 500 && Parent.LocalPlayer.Fuel >= 400)
            {
                bool canBuild = true;
                Laborer builder = ((Laborer)Parent.WorkingPlayer.SelectedUnit);
                MapArea newBuildingArea = new MapArea(builder.X + 1, builder.Y, 2, 2);

                foreach (MapPoint point in newBuildingArea.GetListOfAllMapPoints())
                {
                    if (!Parent.Map.TileIsPassable(UnitLayer.Ground, point.X, point.Y))
                    {
                        canBuild = false;
                    }
                }

                if (canBuild)
                {
                    BuildingUnit b = new PowerPlant(builder.MapPoint.X + 1, builder.MapPoint.Y, Parent.WorkingPlayer);
                    b.IsBuilt = false;
                    Parent.UnitManager.AddUnit(b);
                    builder.BuildBuilding(b);
                    Parent.WorkingPlayer.Fuel = Parent.WorkingPlayer.Fuel - 400;
                    Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 500;
                }
            }
        }
        
        void _turret_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.R, false);
        }

        public void BuildTurret()
        {

            if (Parent.WorkingPlayer.Metal >= 100 && Parent.LocalPlayer.Fuel >= 100) 
            {

                bool canBuild = true;
                Laborer builder = ((Laborer)Parent.WorkingPlayer.SelectedUnit);
                MapArea newBuildingArea = new MapArea(builder.X + 1, builder.Y, 2, 2);

                foreach (MapPoint point in newBuildingArea.GetListOfAllMapPoints())
                {
                    if (!Parent.Map.TileIsPassable(UnitLayer.Ground, point.X, point.Y))
                    {
                        canBuild = false;
                    }
                }

                if (canBuild)
                {
                    BuildingUnit b = new Turret(builder.MapPoint.X + 1, builder.MapPoint.Y, Parent.WorkingPlayer);
                    b.IsBuilt = false;
                    Parent.UnitManager.AddUnit(b);
                    builder.BuildBuilding(b);
                    Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 100;
                    Parent.WorkingPlayer.Fuel = Parent.WorkingPlayer.Fuel - 100;
                }

            }
        }

        void _barrack_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.B, false);
        }

        public void BuildBarrack()
        {
            if (Parent.WorkingPlayer.Metal >= 400)
            {
                bool canBuild = true;
                Laborer builder = ((Laborer)Parent.WorkingPlayer.SelectedUnit);
                MapArea newBuildingArea = new MapArea(builder.X + 1, builder.Y, 2, 2);

                foreach (MapPoint point in newBuildingArea.GetListOfAllMapPoints())
                {
                    if (!Parent.Map.TileIsPassable(UnitLayer.Ground, point.X, point.Y))
                    {
                        canBuild = false;
                    }
                }

                if (canBuild)
                {
                    BuildingUnit b = new Barrack(builder.MapPoint.X + 1, builder.MapPoint.Y, Parent.WorkingPlayer);
                    b.IsBuilt = false;
                    Parent.UnitManager.AddUnit(b);
                    builder.BuildBuilding(b);
                    Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 400;
                }
            }
        }

        void _airStation_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.A, false);
        }

        public void BuildAirStation()
        {
            if (Parent.WorkingPlayer.Metal >= 300 && Parent.LocalPlayer.Fuel >= 300)
            {
                bool canBuild = true;
                Laborer builder = ((Laborer)Parent.WorkingPlayer.SelectedUnit);
                MapArea newBuildingArea = new MapArea(builder.X + 1, builder.Y, 2, 2);

                foreach (MapPoint point in newBuildingArea.GetListOfAllMapPoints())
                {
                    if (!Parent.Map.TileIsPassable(UnitLayer.Ground, point.X, point.Y))
                    {
                        canBuild = false;
                    }
                }

                if (canBuild)
                {
                    BuildingUnit b = new AirStation(builder.MapPoint.X + 1, builder.MapPoint.Y, Parent.WorkingPlayer);
                    b.IsBuilt = false;
                    Parent.UnitManager.AddUnit(b);
                    builder.BuildBuilding(b);
                    Parent.WorkingPlayer.Fuel = Parent.WorkingPlayer.Fuel - 300;
                    Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 300;
                }
            }
        }

        void _fatory_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.F, false);
        }

        public void BuildFactory()
        {
            if (Parent.WorkingPlayer.Metal >= 300 && Parent.WorkingPlayer.Fuel >= 300)
            {
                bool canBuild = true;
                Laborer builder = ((Laborer)Parent.WorkingPlayer.SelectedUnit);
                MapArea newBuildingArea = new MapArea(builder.X + 1, builder.Y, 2, 2);

                foreach (MapPoint point in newBuildingArea.GetListOfAllMapPoints())
                {
                    if (!Parent.Map.TileIsPassable(UnitLayer.Ground, point.X, point.Y))
                    {
                        canBuild = false;
                    }
                }

                if (canBuild)
                {
                    BuildingUnit b = new Factory(builder.MapPoint.X + 1, builder.MapPoint.Y, Parent.WorkingPlayer);
                    b.IsBuilt = false;
                    Parent.UnitManager.AddUnit(b);
                    builder.BuildBuilding(b);
                    Parent.WorkingPlayer.Fuel = Parent.WorkingPlayer.Fuel - 300;
                    Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 300;
                }
            }
        }

        void _laborer_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.L, false);
        }

        public void CreateLaborer()
        {
            if (Parent.LocalPlayer.Metal >= 25)
            {
                ((BuildingUnit)Parent.WorkingPlayer.SelectedUnit).CreateUnit(BuildingCreationQueueType.Laborer);
                Parent.WorkingPlayer.Metal = Parent.LocalPlayer.Metal - 25;
            }
        }

        void _jeep_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.J, false);
        }

        public void CreateJeep()
        {
            if (Parent.WorkingPlayer.Metal >= 50 && Parent.WorkingPlayer.Fuel >= 50)
            {
                ((BuildingUnit)Parent.WorkingPlayer.SelectedUnit).CreateUnit(BuildingCreationQueueType.Jeep);
                Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 50;
                Parent.WorkingPlayer.Fuel = Parent.WorkingPlayer.Fuel - 50;
            }
        }

        void _tank_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.T, false);
        }

        public void CreateTank()
        {
            if (Parent.WorkingPlayer.Metal >= 100 && Parent.WorkingPlayer.Fuel >= 100)
            {
                ((BuildingUnit)Parent.WorkingPlayer.SelectedUnit).CreateUnit(BuildingCreationQueueType.Tank);
                Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 100;
                Parent.WorkingPlayer.Fuel = Parent.WorkingPlayer.Fuel - 100;
            }
        }

        void _robot_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.R, false);
        }

        public void CreateRobot()
        {
            if (Parent.WorkingPlayer.Metal >= 35 && Parent.WorkingPlayer.Fuel >= 10)
            {
                ((BuildingUnit)Parent.WorkingPlayer.SelectedUnit).CreateUnit(BuildingCreationQueueType.Robot);
                Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 35;
                Parent.WorkingPlayer.Fuel = Parent.WorkingPlayer.Fuel - 10;
            }
        }

        void _battleship_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.B, false);
        }

        public void CreateBattleship()
        {
            if (Parent.WorkingPlayer.Metal >= 100 && Parent.WorkingPlayer.Fuel >= 150)
            {
                ((BuildingUnit)Parent.WorkingPlayer.SelectedUnit).CreateUnit(BuildingCreationQueueType.Battleship);
                Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 100;
                Parent.WorkingPlayer.Fuel = Parent.WorkingPlayer.Fuel - 150;
            }
        }

        void _lightship_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.L, false);
        }

        public void CreateLightShip()
        {
            if (Parent.WorkingPlayer.Metal >= 75 && Parent.WorkingPlayer.Fuel >= 75)
            {
                ((BuildingUnit)Parent.WorkingPlayer.SelectedUnit).CreateUnit(BuildingCreationQueueType.LightShip);
                Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 75;
                Parent.WorkingPlayer.Fuel = Parent.WorkingPlayer.Fuel - 75;
            }
        }

        void _soldier_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.S, false);
        }

        public void CreateSoldier()
        {
            if (Parent.WorkingPlayer.Metal >= 40)
            {
                ((BuildingUnit)Parent.WorkingPlayer.SelectedUnit).CreateUnit(BuildingCreationQueueType.Soldier);
                Parent.WorkingPlayer.Metal = Parent.WorkingPlayer.Metal - 40;
            }
        }

        void _generate_OnButtonClick()
        {
            Parent.KeyDown(System.Windows.Forms.Keys.G, false);
        }

        void _inGameButton_OnButtonClick()
        {
            _inGameMenuVisible = true;
        }

        void _continue_OnButtonClick()
        {
            if (_inGameMenuVisible)
            {
                _inGameMenuVisible = false;
            }
        }

        void _player_OnButtonClick()
        {
            if (_inGameMenuVisible)
            {
            }
        }
        
        void _option_OnButtonClick()
        {
            if (_inGameMenuVisible)
            {
                _inGameMenuVisible = false;
                _optionSelected = true;
            }
        }

        void _save_OnButtonClick()
        {
            if (_inGameMenuVisible)
            {
            }
        }

        void _exit_OnButtonClick()
        {
            if (_inGameMenuVisible)
            {
                Audio.StopMusic();
                Globals.iScreenOffsetX = 0;
                Globals.iScreenOffsetY = 0;
                Globals.GameStack.Pop();
                //MainMenu.PlayIntroMusic();
            }
        }

        void _musicOn_OnButtonClick()
        {
            PlayMusic();
        }

        void _musicOff_OnButtonClick()
        {
            Audio.StopMusic();
        }

        void _soundOn_OnButtonClick()
        {
        }

        void _soundOff_OnButtonClick()
        {
        }

        void _back_OnButtonClick()
        {
            if (_optionSelected)
            {
                _inGameMenuVisible = true;
                _optionSelected = false;
            }
        }

        void _startGame_OnButtonClick()
        {
            if (Parent.IsServer)
            {
                Parent.StartMultiplayer();
            }
        }

        #endregion

        #region IRender Members

        public void Render() 
        {
            // Render Bottom Panel
            _sprBottomPanel.Left = 0;
            _sprBottomPanel.Top = Globals.iScreenHeight - _sprBottomPanel.Height + 50;
            _sprBottomPanel.Render(_texBottomPanel);

            _sprBottomPanel.Left += _sprBottomPanel.Width;
            _sprBottomPanel.Render(_texBottomPanel);

            _sprframeWrapper.Left = 78;
            _sprframeWrapper.Bottom = Globals.iScreenHeight - 20;
            _sprframeWrapper.Render(_texframeWrapper);

            _sprframe.Left = 88;
            _sprframe.Bottom = Globals.iScreenHeight - 30;
            _sprframe.Render(_texframe);
            _portrait.Render();

            if (_player != null)
            {
                if (!(_player.SelectedUnit == null))
                {
                    string _hitPoints = "Hit Points: ";
                    _hitPoints += _player.SelectedUnit.HitPoints.ToString();
                    _hitPoints += " / ";
                    _hitPoints += _player.SelectedUnit.MaxHitPoints.ToString();
                    Text.Render(_hitPoints, Color.MidnightBlue, 195, Globals.iScreenHeight - 103);

                    if (_player.SelectedUnit is BuildingUnit)
                    {
                        if (!((BuildingUnit)(_player.SelectedUnit)).IsBuilt)
                        {
                            Text.Render("Percentage Complete:", Color.MidnightBlue, 195, Globals.iScreenHeight - 83);
                            Text.Render(((BuildingUnit)(_player.SelectedUnit)).BuiltPercentFinished.ToString(), Color.MidnightBlue, 375, Globals.iScreenHeight - 83);
                        }
                        if (((BuildingUnit)(_player.SelectedUnit)).CreationPercentFinished != 0)
                        {
                            Text.Render("Percentage Complete:", Color.MidnightBlue, 195, Globals.iScreenHeight - 83);
                            Text.Render(((BuildingUnit)(_player.SelectedUnit)).CreationPercentFinished.ToString(), Color.MidnightBlue, 375, Globals.iScreenHeight - 83);
                        }
                    }

                    if (_player.SelectedUnit is MovableUnit)
                    {
                        Text.Render("Attack:", Color.MidnightBlue, 195, Globals.iScreenHeight - 63);
                        Text.Render(((MovableUnit)(_player.SelectedUnit)).AttackDamage.ToString(), Color.MidnightBlue, 250, Globals.iScreenHeight - 63);

                        Text.Render("Defend:", Color.MidnightBlue, 195, Globals.iScreenHeight - 43);
                        Text.Render(((MovableUnit)(_player.SelectedUnit)).Defense.ToString(), Color.MidnightBlue, 259, Globals.iScreenHeight - 43);
                    }

                    if (_player.SelectedUnit is Laborer)
                        {
                            _builderSelected = true;
                            _factorySelected = false;
                            _barrackSelected = false;
                            _centerSelected = false;
                            _airStationSelected = false;
                            Text.Render("Laborer", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                            int _resource = ((Laborer)_player.SelectedUnit).ResourceAmountGathered / 10;
                            Text.Render(_resource.ToString(), Color.MidnightBlue, 287, Globals.iScreenHeight - 83);
                            Text.Render("Resources:", Color.MidnightBlue, 195, Globals.iScreenHeight - 83);
                            _townCenter.Render();
                            Text.Render("Town Center", Color.Black, 373, Globals.iScreenHeight - 122);
                            _factory.Render();
                            Text.Render("Factory", Color.Black, 395, Globals.iScreenHeight - 87);
                            _airStation.Render();
                            Text.Render("Air Station", Color.Black, 383, Globals.iScreenHeight - 52);
                            _barrack.Render();
                            Text.Render("Barrack", Color.Black, 562, Globals.iScreenHeight - 122);
                            _turret.Render();
                            Text.Render("Turret", Color.Black, 565, Globals.iScreenHeight - 87);
                            _powerPlant.Render();
                            Text.Render("Power Plant", Color.Black, 548, Globals.iScreenHeight - 52);
                        }
                    if (_player.SelectedUnit is TownCenter)
                    {
                        _builderSelected = false;
                        _factorySelected = false;
                        _barrackSelected = false;
                        _centerSelected = true;
                        _airStationSelected = false;
                        Text.Render("TownCenter", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                        if (((BuildingUnit)_player.SelectedUnit).IsBuilt)
                        {
                            _laborer.Render();
                            Text.Render("Laborer", Color.Black, 410, Globals.iScreenHeight - 122);
                        }
                    }
                    if (_player.SelectedUnit is Factory)
                    {
                        _factorySelected = true;
                        _builderSelected = false;
                        _barrackSelected = false;
                        _centerSelected = false;
                        _airStationSelected = false;
                        Text.Render("Factory", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                        if (((BuildingUnit)_player.SelectedUnit).IsBuilt)
                        {
                            _jeep.Render();
                            Text.Render("Jeep", Color.Black, 435, Globals.iScreenHeight - 122);
                            _tank.Render();
                            Text.Render("Tank", Color.Black, 435, Globals.iScreenHeight - 87);
                        }
                        
                    }
                    if (_player.SelectedUnit is Barrack)
                    {
                        _builderSelected = false;
                        _factorySelected = false;
                        _barrackSelected = true;
                        _centerSelected = false;
                        _airStationSelected = false;
                        Text.Render("Barrack", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                        if (((BuildingUnit)_player.SelectedUnit).IsBuilt)
                        {
                            _soldier.Render();
                            Text.Render("Soldier", Color.Black, 430, Globals.iScreenHeight - 122);
                            _robot.Render();
                            Text.Render("Robot", Color.Black, 432, Globals.iScreenHeight - 87);
                        }
                    }
                    if (_player.SelectedUnit is AirStation)
                    {
                        _builderSelected = false;
                        _factorySelected = false;
                        _barrackSelected = false;
                        _centerSelected = false;
                        _airStationSelected = true;
                        Text.Render("AirStation", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                        if (((BuildingUnit)_player.SelectedUnit).IsBuilt)
                        {
                            _lightship.Render();
                            Text.Render("Lightship", Color.Black, 410, Globals.iScreenHeight - 123);
                            _battleship.Render();
                            Text.Render("Battleship", Color.Black, 410, Globals.iScreenHeight - 90);
                        }
                    }
                    if (_player.SelectedUnit is Battleship)
                    {
                        Text.Render("Battleship", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                    }
                    if (_player.SelectedUnit is Jeep)
                    {
                        Text.Render("Jeep", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                    }
                    if (_player.SelectedUnit is LightShip)
                    {
                        Text.Render("LightShip", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                    }
                    if (_player.SelectedUnit is Metal)
                    {
                        Text.Render("Metal", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                    }
                    if (_player.SelectedUnit is OilField)
                    {
                        Text.Render("OilField", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                    }
                    if (_player.SelectedUnit is PowerPlant)
                    {
                        Text.Render("PowerPlant", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                        if (((BuildingUnit)_player.SelectedUnit).IsBuilt)
                        {
                            _generate.Render();
                            if (((PowerPlant)_player.SelectedUnit).IsProducingElectricity)
                                Text.Render("Turn Off", Color.Black, 410, Globals.iScreenHeight - 123);
                            else
                                Text.Render("Turn On", Color.Black, 410, Globals.iScreenHeight - 123);
                        }
                    }
                    if (_player.SelectedUnit is Robot)
                    {
                        Text.Render("Robot", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                    }
                    if (_player.SelectedUnit is Soldier)
                    {
                        Text.Render("Soldier", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                    }
                    if (_player.SelectedUnit is Tank)
                    {
                        Text.Render("Tank", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                    }
                    if (_player.SelectedUnit is Transporter)
                    {
                        Text.Render("Transporter", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                    }
                    if (_player.SelectedUnit is Turret)
                    {
                        Text.Render("Turret", Color.MidnightBlue, 195, Globals.iScreenHeight - 123);
                    }
                }
            }
            
            // Render Top Panel

            _sprTopPanel.Left = 0;
            _sprTopPanel.Top = 0;
            _sprTopPanel.Render(_texTopPanel);

            _sprTopPanel.Left += _sprTopPanel.Width;
            _sprTopPanel.Render(_texTopPanel);
            _inGameMenuButton.Render();
            Text.Render(Parent.LocalPlayer.Name, Color.White, 3, 4);
            Text.Render("Metal:", Color.White,300, 4);
            Text.Render(Parent.LocalPlayer.Metal.ToString(), Color.White, 350, 4);
            Text.Render("Fuel:", Color.White, 400, 4);
            Text.Render(Parent.LocalPlayer.Fuel.ToString(), Color.White, 450, 4);
            Text.Render("Electricity:", Color.White, 500, 4);
            Text.Render(Parent.LocalPlayer.Electricity.ToString(), Color.White, 590, 4);
            Text.Render(_mouseOverText, Color.White, 700, 650);

            if (_inGameMenuVisible)
            {
                _inGameMenuSprite.Render(_inGameMenuTexture);
                _continue.Render();
                //_playerbtn.Render();
                _option.Render();
                //_save.Render();
                _exit.Render();
            }

            if (_optionSelected)
            {
                _inGameMenuSprite.Render(_inGameMenuTexture);
                _music.Render();
                _musicOn.Render();
                _musicOff.Render();
                _sound.Render();
                _soundOn.Render();
                _soundOff.Render();
                _soundVolume.Render();
                _soundDown.Render();
                _soundUp.Render();
                _back.Render();
            }

            if (!Parent.GameHasStarted)
            {

                // Render Game Lobby
                _inGameMenuSprite.Render(_inGameMenuTexture);

                Text.Render(Parent.Players.Count.ToString() + " player(s) in game:", Color.Black, ((Globals.iScreenWidth / 2) - 125), 170);

                for( int i = 0; i < Parent.MultiplayerStatus.PlayerNames.Count; ++i)
                {

                    Text.Render("Player " + (i+1).ToString() + ": " + Parent.MultiplayerStatus.PlayerNames[i], Player.GetColor(i), ((Globals.iScreenWidth / 2) - 125), 200 + (i * 24));

                }

                if (Parent.IsServer)
                {
                    _startGame.Render();
                }
                else
                {
                    Text.Render("Waiting for Server to start...", Color.Black, ((Globals.iScreenWidth / 2) - 125), 460);
                }
            }
        }

        #endregion

        #region IInput Members

        public bool MouseDown(System.Windows.Forms.MouseButtons button, int x, int y)
        {

            if (!Parent.GameHasStarted)
            {
                _startGame.MouseDown(button, x, y);
                return true;
            }

            if (Parent.WorkingPlayer.SelectedUnit != null)
            {
                if (Parent.WorkingPlayer.SelectedUnit is Laborer)
                {
                    if (_inputManagerBuilder.MouseDown(button, x, y))
                        return true;
                }
                else if (Parent.WorkingPlayer.SelectedUnit is BuildingUnit && ((BuildingUnit)Parent.WorkingPlayer.SelectedUnit).IsBuilt)
                {
                    if (Parent.WorkingPlayer.SelectedUnit is Factory)
                    {
                        if (_inputManagerFactory.MouseDown(button, x, y))
                            return true;
                    }
                    else if (Parent.WorkingPlayer.SelectedUnit is Barrack)
                    {
                        if (_inputManagerBarrack.MouseDown(button, x, y))
                            return true;
                    }
                    else if (Parent.WorkingPlayer.SelectedUnit is TownCenter)
                    {
                        if (_inputManagerCenter.MouseDown(button, x, y))
                            return true;
                    }
                    else if (Parent.WorkingPlayer.SelectedUnit is AirStation)
                    {
                        if (_inputManagerAirStation.MouseDown(button, x, y))
                            return true;
                    }
                    else if (Parent.WorkingPlayer.SelectedUnit is PowerPlant)
                    {
                        if (_inputManagerPowerPlant.MouseDown(button, x, y))
                            return true;
                    }
                }
            }

            // NOTE!!! Put all Unit action button handles above this, or multiplayer won't work!

            if (Parent.LocalPlayer != Parent.WorkingPlayer)
                return false;

            else if (_inGameMenuVisible)
            {

                _inputManagerInGameMenu.MouseDown(button, x, y);
                return true;

            }
            else if (_optionSelected)
            {

                _inputManagerOptions.MouseDown(button, x, y);
                return true;
            }
            return _inputManager.MouseDown(button, x, y);

        }

        public bool MouseUp(System.Windows.Forms.MouseButtons button, int x, int y)
        {

            // NOTE!!! Put all Unit action button handles above this, or multiplayer won't work!

            if (Parent.LocalPlayer != Parent.WorkingPlayer)
                return false;

            return _inputManager.MouseUp(button, x, y);
        }

        public bool MouseMove(int x, int y)
        {
            #region Mouse Overs

            #region Builder Mouse Over
            if (_builderSelected)
            {
                if ((x >= (_townCenter.X - _townCenter.Width) && x <= _townCenter.X) && (y >= _townCenter.Y  && y <= (_townCenter.Y + _townCenter.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 500";
                }
                else if ((x >= (_factory.X - _factory.Width) && x <= _factory.X) && (y >= _factory.Y && y <= (_factory.Y + _factory.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 300, Oil 300";
                }
                else if ((x >= (_airStation.X - _airStation.Width) && x <= _airStation.X) && (y >= _airStation.Y && y <= (_airStation.Y + _airStation.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 300, Oil 300";
                }
                else if ((x >= (_barrack.X - _barrack.Width) && x <= _barrack.X) && (y >= _barrack.Y && y <= (_barrack.Y + _barrack.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 400";
                }
                else if ((x >= (_turret.X - _turret.Width) && x <= _turret.X) && (y >= _turret.Y && y <= (_turret.Y + _turret.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 100, Oil 100";
                }
                else if ((x >= (_powerPlant.X - _powerPlant.Width) && x <= _powerPlant.X) && (y >= _powerPlant.Y && y <= (_powerPlant.Y + _powerPlant.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 500, Oil 400";
                }
                else
                {
                    _mouseOverText = "";
                }
            }
            #endregion

            #region Town Center Mouse Over
            else if (_centerSelected)
            {
                if ((x >= (_laborer.X - _laborer.Width) && x <= _laborer.X) && (y >= _laborer.Y && y <= (_laborer.Y + _laborer.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 25";
                }
                else
                {
                    _mouseOverText = "";
                }
            }
            #endregion

            #region Factory mouse Over
            else if (_factorySelected)
            {
                if ((x >= (_jeep.X -_jeep.Width) && x <= _jeep.X) && (y >= _jeep.Y && y <= (_jeep.Y + _jeep.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 50, Oil 50";
                }
                else if ((x >= (_tank.X - _tank.Width) && x <= _tank.X) && (y >= _tank.Y && y <= (_tank.Y + _tank.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 100, Oil 100";
                }
                else
                {
                    _mouseOverText = "";
                }
            }
            #endregion

            #region Airstation Mouse Over
            else if (_airStationSelected)
            {
                if ((x >= (_lightship.X -_lightship.Width) && x <= _lightship.X) && (y >= _lightship.Y && y <= (_lightship.Y + _lightship.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 75, Oil 75";
                }
                else if ((x >= (_battleship.X -_battleship.Width) && x <= _battleship.X) && (y >= _battleship.Y && y <= (_battleship.Y + _battleship.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 100, Oil 150";
                }
                else
                {
                    _mouseOverText = "";
                }
            }
            #endregion

            #region Barrack Mouse Over
            else if (_barrackSelected)
            {
                if ((x >= (_soldier.X - _soldier.Width) && x <= _soldier.X) && (y >= _soldier.Y && y <= (_soldier.Y + _soldier.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 40";
                }
                else if ((x >= (_robot.X -_robot.Width) && x <= _robot.X) && (y >= _robot.Y && y <= (_robot.Y + _robot.Height)))
                {
                    _mouseOverText = "Resources needed: Metal 35, Oil 10";
                }
                else
                {
                    _mouseOverText = "";
                }
            }
            #endregion

            else
            {
                _mouseOverText = "";
            }
            #endregion

            return _inputManager.MouseMove(x, y);
        }

        public bool KeyDown(System.Windows.Forms.Keys key, bool shift)
        {

            // NOTE!!! Put all Unit action button handles above this, or multiplayer won't work!

            if (Parent.LocalPlayer != Parent.WorkingPlayer)
                return false;

            return _inputManager.KeyDown(key, shift);
        }

        public bool KeyUp(System.Windows.Forms.Keys key, bool shift)
        {

            // NOTE!!! Put all Unit action button handles above this, or multiplayer won't work!

            if (Parent.LocalPlayer != Parent.WorkingPlayer)
                return false;

            return _inputManager.KeyUp(key, shift);
        }

        #endregion
    }

}
